package cn.zqh.auth.controller;


import cn.hutool.core.lang.id.NanoId;
import cn.zqh.auth.annotation.RepeatSubmit;
import cn.zqh.auth.config.BSException;
import cn.zqh.auth.domain.Oauth2TokenDto;
import cn.zqh.auth.holder.LoginUserHolder;
import cn.zqh.auth.mapper.UserMapper;
import cn.zqh.auth.service.RouterService;
import cn.zqh.auth.service.UserService;
import cn.zqh.bean.api.CommonResult;
import cn.zqh.bean.api.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 自定义Oauth2获取令牌接口
 * Created by macro on 2020/7/17.
 */

@RequestMapping("/oauth")
@CrossOrigin
public class AuthController {

    @Autowired
    private TokenEndpoint tokenEndpoint;
    @Resource
    private UserMapper userMapper;
    @Autowired
    private LoginUserHolder loginUserHolder;
    @Resource
    private UserService userService;
    @Autowired
    private RouterService routerService;

    @Value("${spring.mail.username}")
    private String username;   // 邮件发送人

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Resource
    private JavaMailSenderImpl mailSender;



    /**
      * @Author: zqh
      * @Date: 
      * @Description: 登录认证
    */
    @RequestMapping(value = "/token", method = RequestMethod.POST)
    public CommonResult<Oauth2TokenDto> postAccessToken(Principal principal, @RequestParam Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {
        OAuth2AccessToken oAuth2AccessToken = tokenEndpoint.postAccessToken(principal, parameters).getBody();
        System.out.println(principal.getName());
        String tokenValue = oAuth2AccessToken.getValue();
        String refreshTokenValue = oAuth2AccessToken.getRefreshToken().getValue();
        Oauth2TokenDto oauth2TokenDto = Oauth2TokenDto.builder()
                .token(oAuth2AccessToken.getValue())
                .refreshToken(oAuth2AccessToken.getRefreshToken().getValue())
                .expiresIn(oAuth2AccessToken.getExpiresIn())
                .tokenHead("Bearer ").build();
        return CommonResult.success(oauth2TokenDto);
    }



    /**
      * @Author: zqh
      * @Date: 
      * @Description: 获取当前登录用户的信息
    */
    @GetMapping("/currentUser")
    public CommonResult currentUser(){
        return CommonResult.success(loginUserHolder.getCurrentUser());
    }





}
